/**
 * 
 */
package orfeu.engine;

import javax.microedition.khronos.opengles.GL10;
import javax.microedition.khronos.opengles.GL11;
import javax.microedition.khronos.opengles.GL11Ext;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.opengl.GLUtils;

/**
 * @author Tony
 *
 */
public class Background extends Sprite{

	public Background(float w, float h, int nf, long fp, int res)
	{
		super(w,h,nf,fp,res);
		px = 0;
		py = 0;
	}
	
	/*
	@Override
	*//** function that loads a texture **//*
	public void loadTexture(GL10 gl, Context context) {
		int id = getTextureId(gl);
		
		// flip the texture vertically
		Matrix flip = new Matrix();
		flip.postScale(1f, -1f);
		
		// tell BitmapFactory not to scale image
		BitmapFactory.Options opts = new BitmapFactory.Options();
		opts.inScaled = true;
		
		// load and flip the texture
		Bitmap temp = BitmapFactory.decodeResource(context.getResources(), resource, opts);
		spriteImage = Bitmap.createBitmap(temp, 0, 0, temp.getWidth(), temp.getHeight(), flip, true);
		temp.recycle();
		
		gl.glBindTexture(GL10.GL_TEXTURE_2D, id);
		
		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR_MIPMAP_NEAREST);
		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR_MIPMAP_NEAREST);
		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_REPEAT);
		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_REPEAT);
		
		
		// generate and load mipmaps
		//int[] crop = {0, spriteImage.getWidth(), spriteImage.getHeight(), -spriteImage.getHeight()};
		//((GL11) gl).glTexParameteriv(GL10.GL_TEXTURE_2D, GL11Ext.GL_TEXTURE_CROP_RECT_OES, crop, 0);
		for(int level=0, height = spriteImage.getHeight(), width = spriteImage.getWidth(); true; level++) {
	        // push the bitmap onto the GPU:
	        GLUtils.texImage2D(GL10.GL_TEXTURE_2D, level, spriteImage, 0);
	        
	        // we need to stop when the texture is 1x1:
	        if(height==1 && width==1) break;
	        
	        // resize, and let's go again:
	        width >>= 1; height >>= 1;
	        if (width < 1)  
	        	width = 1;
	        if (height < 1) 
	        	height = 1;
	        
	        Bitmap bmp2 = Bitmap.createScaledBitmap(spriteImage, width, height, true);
	        spriteImage.recycle();
	        spriteImage = bmp2;
	    }
	    
		spriteImage.recycle();
	    
	    textureId = id;	
	}*/
	
	@Override
	/** draw function to display the quad **/
	public void draw(GL10 gl) {
		gl.glPushMatrix();
		gl.glFrontFace(GL10.GL_CCW);
		gl.glEnable(GL10.GL_CULL_FACE);
		gl.glCullFace(GL10.GL_BACK);

		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
		gl.glEnable(GL10.GL_TEXTURE_2D);
        gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
        gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, texcoordBuffer);
        gl.glBindTexture(GL10.GL_TEXTURE_2D, textureId);
        
        
		gl.glTranslatef(px, py, 0);
		gl.glRotatef(rx, 1, 0, 0);
		gl.glRotatef(ry, 0, 1, 0);
		gl.glScalef(sx, sy, 1);
		
		
		gl.glDrawElements(GL10.GL_TRIANGLES, indices.length, GL10.GL_UNSIGNED_SHORT, indexBuffer);

		gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
		gl.glDisable(GL10.GL_CULL_FACE);
		gl.glPopMatrix();
	}
	
}
